package com.ruoyi.project.system.table.controller;

import com.ruoyi.common.utils.poi.CustomExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.exception.DataException;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.table.domain.BusiTable;
import com.ruoyi.project.system.table.domain.Table;
import com.ruoyi.project.system.table.service.IBusiTableService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
//import sun.applet.Main;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

/**
*
* @Author: 郑吉志
*
* @Create: 23.2.22
**/
@Controller
@RequestMapping("/system/table")
public class BusiTableController extends BaseController
{
    private String prefix = "system/table";
    @Autowired
    private IBusiTableService busiTableService;
//使用了@RequiresPermissions注解来限制该方法访问的权限。如果当前用户没有“system:table:view”这个权限，就无法访问该方法。
    @RequiresPermissions("system:table:view")
    @GetMapping()
    public String table()
    {
        return prefix + "/table";
    }

    /**
     * 查询年度一览表生成列表
     */
    @RequiresPermissions("system:table:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Table table,Integer pageNum,Integer pageSize)
    {
        startPage();
        List<BusiTable> list = busiTableService.selectList(table,pageNum,pageSize);
        return getDataTable(list);
    }

    /**
     * 单个导出
     */
    @GetMapping("/singleExcel")
    @ResponseBody
    public AjaxResult singleExcel(String id) throws IOException {
        String filename = busiTableService.singleExport(id);
        return AjaxResult.success(filename);
    }
    /**
     * 导出农村基本信息采集列表
     * 导出全部
     */
    @Log(title = "农村基本信息采集", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/exportAll",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public AjaxResult exportAll() throws Exception {
        String filename = busiTableService.exportAll();
        return AjaxResult.success(filename);
    }
    //导入模板
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
       //存放模板的路径,linux写法
//        String str="/root/prod/ruoyi/template";
        //win写法
        File tmp = new File("");
        String filePath = tmp.getCanonicalPath();
        String str=filePath+"\\src\\main\\resources\\static\\存放模板";
        File path=new File(str);
        if (!path.exists())
            path.mkdirs();
        try {
            file.transferTo(new File(path, Objects.requireNonNull(file.getOriginalFilename())));
        }catch (IOException e){
            throw new DataException("文件已存在请更改文件名后上传");
        }
        return AjaxResult.success();
    }
    //下载模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() throws IOException {
        CustomExcelUtil excelUtil=new CustomExcelUtil();
        //linux下载路径
//      String filePath="/home/ruoyi/uploadPath/一览表模板.xlsx";
        //win下载路径
        File file = new File("");
        String path = file.getCanonicalPath();
        String filePath=path+"/src/main/resources/static/Template2/一览表模板.xlsx";
        InputStream inputStream = new FileInputStream(filePath);
        XSSFWorkbook workbook= new XSSFWorkbook(inputStream);
        OutputStream out;
        String filename = "一览表模板.xlsx";
        out = Files.newOutputStream(Paths.get(excelUtil.getAbsoluteFile(filename)));
        workbook.write(out);
        return AjaxResult.success(filename);
    }
    /**
     * 选择导出
     * @return 文件名称
     */
    @GetMapping("/selectExport")
    @ResponseBody
    public AjaxResult selectExport(String ids) throws IOException {
        String filename = busiTableService.batchExport(ids);
        return AjaxResult.success(filename);
    }

}
